﻿#include<stdio.h>
#include<stdlib.h>
typedef struct lstack {
	int data;
}LS,*ls;//线性栈
int GetInt();
ls StackBuild(int length);
int WetherEmpty(ls top, ls base);
int WetherFull(ls top, ls base, int length);
ls Push(ls top, ls base, int length);
ls Pop(ls top, ls base);
int main() {
	int length = GetInt();
	ls base = StackBuild(length);
	ls top = base;
	while (GetInt()) {//是否结束交互
		printf("清空/出/入栈   0/1/2：");
		int k = GetInt();
		if (k == 2) {//选择操作
			do {//是否结束入栈
				top = Push(top, base, length);
				printf("是否继续 1/0：");
			} while (GetInt());
		}
		else if (k == 1) {
			do {//是否结束出栈
				top = Pop(top, base);
				printf("是否继续 1/0：");
			} while (GetInt());
		}
		else if (k == 0) { 
			base = top; 
		}
		printf("是否继续    1/0：");
	}
	return 0;
}
//便捷输入
int GetInt() {
	int n;
	scanf_s("%d", &n);
	return n;
}
//栈建立
ls StackBuild(int length) {
	ls base = (ls)malloc(sizeof(ls) * length);
	return base;
}
//栈空判断
int WetherEmpty(ls top, ls base) {//top：栈顶指针
	if (top == base)return 1;
	return 0;
}
//栈满判断
int WetherFull(ls top, ls base, int length) {
	if (top == base + length)return 1;
	return 0;
}
//入栈操作
ls Push(ls top, ls base, int length) {
	if (WetherFull(top, base, length)) {
		printf("ERROR!\n");
		return top;
	}
	top->data = GetInt();
	return ++top;
}
//出栈操作
ls Pop(ls top, ls base) {
	if (WetherEmpty(top, base)) {
		printf("ERROR!\n");
		return top;
	}
	printf("  %d  ", (top-1)->data);
	return --top;
}
